from django.shortcuts import render
from pyhanlp import *

NLPTokenizer = JClass('com.hankcs.hanlp.tokenizer.NLPTokenizer')
test = '中国科学院计算技术研究所的宗成庆教授正在教授自然语言处理课程'
test_segment = NLPTokenizer.segment(test)
print(test_segment)

# 词性标注与中文名称的映射
POS_MAP = {
    "v": {"description": "动词", "color": "#FF6347"},  # 高亮动词
    "vi": {"description": "不及物动词（内动词）", "color": "#FF6347"},
    "vf": {"description": "趋向动词", "color": "#FF6347"},
    "vn": {"description": "名动词", "color": "#FF6347"},
    "vshi": {"description": "动词‘是’", "color": "#FF6347"},
    "vyou": {"description": "动词‘有’", "color": "#FF6347"},
    "n": {"description": "名词", "color": "#4682B4"},  # 高亮名词
    "nr": {"description": "人名", "color": "#4682B4"},
    "ns": {"description": "地名", "color": "#4682B4"},
    "nb": {"description": "生物名", "color": "#4682B4"},
    "nf": {"description": "食品", "color": "#4682B4"},
    "ni": {"description": "机构相关", "color": "#4682B4"},
    "a": {"description": "形容词", "color": "#FFD700"},  # 高亮形容词
    "ag": {"description": "形容词性语素", "color": "#FFD700"},
    "al": {"description": "形容词性惯用语", "color": "#FFD700"},
    "d": {"description": "副词", "color": "#32CD32"},
    "dg": {"description": "副词性语素", "color": "#32CD32"},
    "r": {"description": "代词", "color": "#8A2BE2"},
    "rr": {"description": "人称代词", "color": "#8A2BE2"},
    "rz": {"description": "指示代词", "color": "#8A2BE2"},
    "ry": {"description": "疑问代词", "color": "#8A2BE2"},
    "p": {"description": "介词", "color": "#20B2AA"},  # 较低重要性的介词
    "c": {"description": "连词", "color": "#20B2AA"},
    "u": {"description": "助词", "color": "#20B2AA"},
    "w": {"description": "标点符号", "color": "#808080"},
    "wb": {"description": "百分号/千分号", "color": "#DC143C"},
    "wd": {"description": "逗号", "color": "#DC143C"},
    "ws": {"description": "省略号", "color": "#DC143C"},
    "m": {"description": "数词", "color": "#FF8C00"},  # 数词颜色较为醒目
    "mg": {"description": "数语素", "color": "#FF8C00"},
    "q": {"description": "量词", "color": "#A52A2A"},  # 量词使用棕色
    "qt": {"description": "时量词", "color": "#A52A2A"},
    "x": {"description": "其他词性", "color": "#808080"},  # 灰色
    "xu": {"description": "网址URL", "color": "#808080"},
    "xx": {"description": "非语素字", "color": "#808080"}
}


def pos_tagging(request):
    initial_example = "法院裁定被告李四在三个月内支付欠款人民币五万元，并承担本案的全部诉讼费用."  # 初始例句
    user_input = initial_example  # 默认输入
    highlighted_text = ""
    pos_tags = None
    show_result = False

    if request.method == 'POST':
        show_result = True
        text = request.POST['text']
        user_input = text
        if text:
            # 使用 HanLP 进行词性识别
            segments = NLPTokenizer.segment(user_input)

            # 提取词和词性信息
            for item in segments:
                word = item.word  # 获取词
                pos = item.nature.toString().strip().lower()  # 获取词性
                pos_info = POS_MAP.get(pos, {"description": pos, "color": "#000000"})
                pos_chinese = pos_info["description"]
                color = pos_info["color"]
                highlighted_text += f'<span class="word" style="color:{color}" title="{pos_chinese}">{word}（{pos_chinese}）</span>'

    return render(request, 'PosTagger/PosTagger.html', {
        'pos_tags': pos_tags,
        'highlighted_text': highlighted_text,
        'user_input': user_input,
        'show_result': show_result,
    })
